<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 数据恢复
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container v2-group">
            <div class="handle-box">
                <el-input v-model="query.kw" placeholder="关键词" class="handle-input mr10" clearable></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
                <el-button
                    type="success"
                    icon="el-icon-plus"
                    class="handle-del mr10"
                    @click="handleAdd"
                >新增恢复任务</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
                stripe
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column prop="task" width="255" label="恢复任务"></el-table-column>
                <el-table-column prop="update_time" label="恢复时间" width="170"></el-table-column>
                <el-table-column label="恢复数据库" show-overflow-tooltip>
                    <template slot-scope="scope">{{scope.row.name}}</template>
                </el-table-column>
                <el-table-column label="操作人员" width="150">
                    <template slot-scope="scope">{{scope.row.username}}</template>
                </el-table-column>
                <el-table-column label="文件大小" width="150">
                    <template slot-scope="scope">{{scope.row.size}}</template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, sizes, prev, pager, next, jumper"
                    :current-page="query.page"
                    :page-size="query.size"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                    :page-sizes="[10, 20, 30, 40, 50, 100]"
                    @size-change="handleSizeChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="530px">
            <el-form ref="entityAdd" :model="form" :rules="rulesEntityAdd" label-width="120px">
                <el-form-item label="备份文件" width="100px" prop="file">
                    <el-upload
                        class="upload-demo"
                        drag
                        ref="upload"
                        :auto-upload="false"
                        :on-change="handleFileChange"
                        action=""
                        :limite="1"
                        :multiple="false">
                        <i class="el-icon-upload"></i>
                        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                        <div class="el-upload__tip" slot="tip">只能上传文件不超过500kb</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="任务名称" prop="task_name">
                    <el-input v-model="form.task_name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import api from '../../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                address: '',
                name: '',
                kw: '',
                page: 1,
                size: 10
            },
            tableData: [
                {
                    task: 'restore2024_1101',
                    name: '1101.db',
                    surname: '张三',
                    update_time: '2024-11-01 00:00:00',
                    size: '12KB'
                }
            ],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            addVisible: false,
            rulesEntityAdd: {
                file: [{ required: false, message: '请上传文件', trigger: 'blur' }],
                task_name: [{ required: true, message: '请输入任务名称', trigger: 'blur' }]
            }
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            api.getRestorations(this.query).then(res => {
                this.tableData = res.items;
                this.pageTotal = res.total;
            }).catch(err => {
                this.$message.error(err);
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'page', 1);
            this.getData();
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 新增操作
        handleAdd() {
            this.form = {};
            this.addVisible = true;
            if (this.$refs.upload !== undefined) {
                this.$refs.upload.clearFiles();
            }
        },
        //新增操作
        saveAdd() {
            if (this.form.file === undefined || Object.keys(this.form.file).length === 0) {
                this.$message.info('请上传文件');
                return;
            }
            this.$refs.entityAdd.validate(valid => {
                if (valid) {
                    api.addRestoration({task_name: this.form.task_name, file: this.form.file.raw}).then(res => {
                        this.$message.success(`新增成功`);
                        this.addVisible = false;
                        this.getData();
                    }).catch(err => {
                        this.$message.error(err);
                    });
                } else {
                    return false;
                }
            });
        },
        handleFileChange(file, fileList) {
            this.form.file = file;
            if (fileList.length > 1) {
                fileList.splice(0, 1);
            }
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        handleSizeChange(val) {
            this.$set(this.query, 'size', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
